import tkinter as tk
from tkinter import ttk
from tkinter import simpledialog
window=tk.Tk()
window.title("To Do List")
window.geometry("800x600")
columns=["To_Do","In Progress","Done"]
frames={}
for col in columns:
    frame = tk.Frame(window, relief=tk.RAISED, borderwidth=1)
    frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    frames[col] = frame
    label = tk.Label(frame, text=col, bg='lightblue', font=('Arial', 12, 'bold'))
    label.pack(fill=tk.X)

def create_card_dialog():
    # 创建一个新的顶级窗口
    dialog = tk.Toplevel(window)
    dialog.title("Create New Task")
    dialog.geometry("400x300")
    
    # 输入框变量
    task_name_var = tk.StringVar()
    priority_var = tk.StringVar()
    due_data_var = tk.StringVar()

    # 任务名称
    task_name_label = tk.Label(dialog, text="Task Name:")
    task_name_label.pack(pady=5)
    task_name_entry = tk.Entry(dialog, textvariable=task_name_var)
    task_name_entry.pack(pady=5)
    
    # 优先级
    priority_label = tk.Label(dialog, text="Priority:")
    priority_label.pack(pady=5)
    priority_entry = tk.Entry(dialog, textvariable=priority_var)
    priority_entry.pack(pady=5)
    
    # 截止日期
    due_data_label = tk.Label(dialog, text="Due Date:")
    due_data_label.pack(pady=5)
    due_data_entry = tk.Entry(dialog, textvariable=due_data_var)
    due_data_entry.pack(pady=5)
    
    # 确认按钮
    confirm_button = tk.Button(dialog, text="Confirm", command=lambda: add_task(task_name_var.get(), priority_var.get(), due_data_var.get(), dialog))
    confirm_button.pack(pady=10)

def add_task(task_name, priority, due_data, dialog):
    if not task_name or not priority or not due_data:
        return
    create_card(frames["To_Do"], task_name, priority, due_data)
    dialog.destroy()  # 关闭对话框

def create_card(frame, task_name, priority, due_data):
    card_frame = tk.Frame(frame, relief=tk.RAISED, borderwidth=1, padx=10, pady=10)
    card_frame.pack(fill=tk.X, pady=5)
    
    task_label = tk.Label(card_frame, text=f"Task: {task_name}", anchor='w')
    task_label.pack(fill=tk.X)
    priority_label = tk.Label(card_frame, text=f"Priority: {priority}", anchor='w')
    priority_label.pack(fill=tk.X)
    due_data_label = tk.Label(card_frame, text=f"Due data: {due_data}", anchor='w')
    due_data_label.pack(fill=tk.X)
    edit_button = tk.Button(card_frame, text="edit", command=lambda:edit_task(task_name,card_frame))
    edit_button.pack(anchor='s')
    delete_button = tk.Button(card_frame, text="delete", command=lambda: delete_card(card_frame))
    delete_button.pack(anchor='s')

def delete_card(card_frame):
    card_frame.destroy()


def edit_task(task_name, card_frame):
    
    new_task_name = simpledialog.askstring("Edit task name", f"Current task: {task_name}\nPlease enter new task name")
    if new_task_name:
        
        for widget in card_frame.winfo_children():
            if isinstance(widget, tk.Label) and widget.cget('text').startswith("Task:"):
                widget.config(text=f"Task: {new_task_name}")

button=tk.Button(window,text="create",command=create_card_dialog)
button.pack()
window.mainloop()